package cn.hl.hm.controller;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hl.hm.entity.backstage.BackStageMenus;
import cn.hl.hm.entity.backstage.BackStageRoleMenu;
import cn.hl.hm.entity.backstage.BackStageRoles;
import cn.hl.hm.service.BackstageAuthService;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;
import net.sf.json.JSONArray;

/**
 * 
 * @author dahe
 * 权限管理（管理后台）
 *
 */
@Controller
@RequestMapping("/backstageAuthController")
public class BackstageAuthController {
	private static final Logger logger = LoggerFactory.getLogger(OrderController.class);


	@Autowired
	private BackstageAuthService backstageAuthService;
	
	
	

	

	
	
	//添加角色和权限
	@RequestMapping(value = "/addRoleMenu", method = RequestMethod.POST)
	public @ResponseBody String addRoleMenu ( 
			BackStageRoleMenu backStageRoleMenu,
			String rmsList,//与实体类对应
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("权限---添加、修改角色权限---backstageAuthController/addRoleMenu");
//		BackStageRoles backStageRoles = new BackStageRoles();
//		backStageRoles.setRoleName(roleName);
		//根据用户名查id
//		BackStageRoles Roles =  backstageAuthService.selectByroleName(backStageRoles);
//		int id = Roles.getId();
		
		
		
//		[ {"menuId":"1","rmFlag":"0"},
//		  {"menuId":"2","rmFlag":"0"},
//		  {"menuId":"3","rmFlag":"1"},
//  	  {"menuId":"4","rmFlag":"1"}
//		]
		JSONArray jsonArray = JSONArray.fromObject(rmsList);//把String转换为json 
		List<BackStageRoleMenu> list = JSONArray.toList(jsonArray,BackStageRoleMenu.class);//这里的t是Class<T> 
		
		List<BackStageRoleMenu> rmList = backstageAuthService.selectInfo(backStageRoleMenu);
		if(rmList!=null&&rmList.size()>0){
			
			  backstageAuthService.deleteByRoleId(backStageRoleMenu.getRoleId());
		} 
			
		for (BackStageRoleMenu roleMenu : list) {
			backStageRoleMenu.setMenuId(roleMenu.getMenuId());
			backStageRoleMenu.setRoleId(backStageRoleMenu.getRoleId());
			backStageRoleMenu.setRmFlag(roleMenu.getRmFlag());
			//Date d = new Date();
			backStageRoleMenu.setCreateTime(new Date());
			backstageAuthService.insertRoleMenu(backStageRoleMenu);
		}
		Result.ok(null, InfoCode.SUCCESS, response);
		return null;
			
	}
	
	
	//角色权限查询
	@RequestMapping(value = "/selectRoleMenu", method = RequestMethod.POST)
	public @ResponseBody String selectRoleMenu (
			BackStageRoleMenu record,
			HttpServletRequest request, HttpServletResponse response) {
				 
		logger.info("权限---角色权限查询---backstageAuthController/selectRoleMenu");
		List<BackStageRoleMenu> rmList = backstageAuthService.selectInfo(record);
		if(rmList!=null&&rmList.size()>0){
			Result.ok(rmList, InfoCode.SUCCESS, response);
			return null;
		}else {
			Result.ok(null, InfoCode.SUCCESS, response);
			return null;
		}
		
		
	}
	
	//------------------------------------------------------------------------------------
	@RequestMapping(value = "/selectUserAuths", method = RequestMethod.POST)
	public @ResponseBody String selectUserAuths(@RequestParam(value = "userId", required = true) String userId, // 手机号,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			List<BackStageMenus> selectUserAuths = backstageAuthService.selectUserAuths(4);
			if(selectUserAuths==null){
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;
			}
			Result.ok(selectUserAuths, InfoCode.FAILURE, response);
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.SUCCESS, response);
		}
		return null;
	}
	
	@RequestMapping(value = "/selectRoles", method = RequestMethod.POST)
	public @ResponseBody String selectRoles(
			HttpServletRequest request, HttpServletResponse response) {
		logger.info("权限---角色查询---backstageAuthController/selectRoles");
		try {
			 List<BackStageRoles> selectRoles = backstageAuthService.selectRoles();
			if(selectRoles==null){
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;
			}
			Result.ok(selectRoles, InfoCode.FAILURE, response);
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.SUCCESS, response);
		}
		return null;
	}
	@RequestMapping(value = "/selectMenus", method = RequestMethod.POST)
	public @ResponseBody String selectMenus(
			HttpServletRequest request, HttpServletResponse response) {
		try {
			List<BackStageMenus> selectMenus = backstageAuthService.selectMenus();
			if(selectMenus==null){
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;
			}
			Result.ok(selectMenus, InfoCode.FAILURE, response);
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null,InfoCode.SUCCESS, response);
		}
		return null;
	}
	@RequestMapping(value = "/selectUserRoles", method = RequestMethod.POST)
	public @ResponseBody String selectUserRoles(@RequestParam(value = "userId", required = true) Integer userId, 
			HttpServletRequest request, HttpServletResponse response) {
		try {
			List<BackStageRoles> roles = backstageAuthService.selectUserRoles(userId);
			if(roles==null){
				Result.ok(null, InfoCode.SUCCESS, response);
				return null;
			}
			Result.ok(roles, InfoCode.FAILURE, response);
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.SUCCESS, response);
		}
		return null;
	}
	@RequestMapping(value = "/addRoles", method = RequestMethod.POST)
	public @ResponseBody String addRoles(BackStageRoles role,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			int i  = backstageAuthService.addRoles(role);
			if(i>0) {
				Result.ok(null, InfoCode.SUCCESS, response);
			}else {
				Result.ok(null, InfoCode.FAILURE, response);
			}
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.SUCCESS, response);
		}
		return null;
	}
	@RequestMapping(value = "/addMenus", method = RequestMethod.POST)
	public @ResponseBody String addMenus(BackStageMenus menu, 
			HttpServletRequest request, HttpServletResponse response) {
		try {
			int i  = backstageAuthService.addMenus(menu);
			
			if(i>0) {
				Result.ok(null, InfoCode.SUCCESS, response);
			}else {
				Result.ok(null, InfoCode.FAILURE, response);
			}
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.SUCCESS, response);
		}
		return null;
	}
	@RequestMapping(value = "/addUserRoles", method = RequestMethod.POST)
	public @ResponseBody String addUserRoles(@RequestParam(value = "userId", required = true) Integer userId, 
			@RequestParam(value = "roleId", required = true) Integer roleId, 
			HttpServletRequest request, HttpServletResponse response) {
		try {
			int i = backstageAuthService.addUserRoles(userId,roleId);
			if(i>0) {
				Result.ok(null, InfoCode.SUCCESS, response);
			}else {
				Result.ok(null, InfoCode.FAILURE, response);
			}
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.SUCCESS, response);
		}
		return null;
	}

//	@RequestMapping(value = "/addRoleMenu", method = RequestMethod.POST)
//	public @ResponseBody String addRoleMenu(@RequestParam(value = "roleId", required = true) Integer roleId, 
//			@RequestParam(value = "menuId", required = true) Integer menuId,
//			HttpServletRequest request, HttpServletResponse response) {
//		try {
//			int i = backstageAuthService.addRoleMenu(menuId,roleId);
//			if(i>0) {
//				Result.ok(null, InfoCode.SUCCESS, response);
//			}else {
//				Result.ok(null, InfoCode.FAILURE, response);
//			}
//			return null;
//
//		} catch (Exception e) {
//			e.printStackTrace();
//			Result.ok(null, InfoCode.SUCCESS, response);
//		}
//		return null;
//	}
	@RequestMapping(value = "/deleteRoleMenu", method = RequestMethod.POST)
	public @ResponseBody String deleteRoleMenu(@RequestParam(value = "id", required = true) Integer id, 
			HttpServletRequest request, HttpServletResponse response) {
		try {
			int i = backstageAuthService.deleteRoleMenu(id);
			if(i>0) {
				Result.ok(null, InfoCode.SUCCESS, response);
			}else {
				Result.ok(null, InfoCode.FAILURE, response);
			}
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.SUCCESS, response);
		}
		return null;
	}
	@RequestMapping(value = "/deleteUserRoles", method = RequestMethod.POST)
	public @ResponseBody String deleteUserRoles(@RequestParam(value = "id", required = true) Integer id, 
			HttpServletRequest request, HttpServletResponse response) {
		try {
			int i = backstageAuthService.deleteUserRoles(id);
			if(i>0) {
				Result.ok(null, InfoCode.SUCCESS, response);
			}else {
				Result.ok(null, InfoCode.FAILURE, response);
			}
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.SUCCESS, response);
		}
		return null;
	}
	@RequestMapping(value = "/updateRoleInfo", method = RequestMethod.POST)
	public @ResponseBody String updateRoleInfo(@RequestParam(value = "role", required = true)BackStageRoles role, 
			HttpServletRequest request, HttpServletResponse response) {
		try {
			int i = backstageAuthService.updateRoleInfo(role);
			if(i>0) {
				Result.ok(null, InfoCode.SUCCESS, response);
			}else {
				Result.ok(null, InfoCode.FAILURE, response);
			}
			
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.SUCCESS, response);
		}
		return null;
	}
	
	@RequestMapping(value = "/updateMenuInfo", method = RequestMethod.POST)
	public @ResponseBody String updateMenuInfo(@RequestParam(value = "menu", required = true)BackStageMenus menu, 
			HttpServletRequest request, HttpServletResponse response) {
		try {
			int i = backstageAuthService.updateMenuInfo(menu);
			if(i>0) {
				Result.ok(null, InfoCode.SUCCESS, response);
			}else {
				Result.ok(null, InfoCode.FAILURE, response);
			}
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.SUCCESS, response);
		}
		return null;
	}
	
	
	@RequestMapping(value = "/selectAllRoleMenus", method = RequestMethod.POST)
	public @ResponseBody String selectAllRoleMenus(
			HttpServletRequest request, HttpServletResponse response) {
		try {
			Result.ok(backstageAuthService.selectRoleMenus(), InfoCode.FAILURE, response);
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.SUCCESS, response);
		}
		return null;
	}
	
	@RequestMapping(value = "/selectAllUserRoles", method = RequestMethod.POST)
	public @ResponseBody String selectAllUserRoles(
			HttpServletRequest request, HttpServletResponse response) {
		try {
			Result.ok(backstageAuthService.selectUserRoles(), InfoCode.FAILURE, response);
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			Result.ok(null, InfoCode.SUCCESS, response);
		}
		return null;
	}


}
